package org.nlp2rdf.navigator.component.resource.maps;

import java.util.Set;

import org.apache.log4j.Logger;
import org.nlp2rdf.navigator.component.exception.ComponentInitException;
import org.nlp2rdf.navigator.util.Prefixer;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class MapFromOntology extends AbstractMap implements IMap {
	private static final Logger logger = Logger.getLogger(MapFromOntology.class);

	private Set<String> ontologyUrls;
	private Prefixer prefixer = null;
    //changes uris of individuals and attaches the comment of a class to it (quite a hack for Olia!)
	private boolean tweakIndividuals = false;

	@Override
	protected void _init() throws ComponentInitException {
		try {
			OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, ModelFactory
					.createDefaultModel());
			for (String ontologyUrl : ontologyUrls) {
				m.read(ontologyUrl);
			}
			for (Object tmp : m.listClasses().toList()) {
				OntClass c = (OntClass) tmp;
				if (c.isAnon()) {
					continue;
				}
				myMap.put(c.getURI(), c.getComment(null));
				// if(c.getURI().equals("http://nachhalt.sfb632.uni-potsdam.de/owl/stts.owl#FiniteAuxiliaryVerb")){
				// System.out.println(c.getComment(null));
				// }

			}
			if (prefixer != null && tweakIndividuals) {
				for (Object tmp : m.listIndividuals().toList()) {
					Individual i = (Individual) tmp;
					OntClass cl = i.getOntClass(true);
					myMap.put(prefixer.getBase() + i.getLocalName(), cl.getComment(null));
				}
			}
		} catch (Exception e) {
			logger.error(e);
			throw new ComponentInitException("Error in init method " + getClass().getSimpleName(), e);
		}

	}

	public void setPrefixer(Prefixer prefixer) {
		this.prefixer = prefixer;
	}

	public void setOntologyUrls(Set<String> ontologyUrls) {
		this.ontologyUrls = ontologyUrls;
	}

	public void setTweakIndividuals(boolean tweakIndividuals) {
		this.tweakIndividuals = tweakIndividuals;
	}

}
